# 多线程
from lxml import etree
import requests
import threading

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63"}
url = 'https://www.5tps.com/html/23602.html'
html = etree.HTML(requests.get(url, headers=headers).content)
results = html.xpath('//ul/li/a/@href')  # 音频链接
resultst = html.xpath('//ul/li/a/text()')  # 章节名字
# print(results)
# print(resultst)

for i in range(len(results)):
    results[i] = 'https://www.5tps.com/' + results[i]
    resultst[i] = resultst[i].replace('\xa0', '')


def save_file(result, resultt):
    with open('E:\data\pingshu3\{}.mp3'.format(resultt[1:-1]), 'wb') as f:
        f.write(requests.get(result).content)  # 下载并保存，具体保存路径根据需要修改
    print(resultt[1:-1])


def main():
    for i in zip(results, resultst):
        # 创建线程
        thread_spider = threading.Thread(target=save_file, args=(i[0], i[1]))
        # 启动线程
        thread_spider.start()


if __name__ == '__main__':
    main()